package com.lightjet.macross.base.upms.service.impl;

import com.lightjet.macross.base.feign.api.domain.AuthRole;
import com.lightjet.macross.base.feign.api.domain.AuthUser;
import com.lightjet.macross.base.upms.domain.SysUser;
import com.lightjet.macross.base.upms.service.LoginAdapterService;
import com.lightjet.macross.base.upms.service.SysUserService;
import com.lightjet.macross.common.util.MD5Util;
import lombok.extern.java.Log;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.List;

@Log
@Service("macross")
@Primary
public class MacrossLoginAdapterServiceImpl implements LoginAdapterService {

    @Resource
    JdbcTemplate bizJdbcTemplate;

    @Resource
    SysUserService sysUserService;

    @Override
    public String GetEncodedPwd(String original, String salt, Object args) {
        //org.springframework.security.authentication
        return MD5Util.Encode(original,salt);  //new Md5Hash(original, salt, HASH_ITERATIONS).toString();
    }

    @Override
    public AuthUser Authenticate(String username,String proxyed) {
        SysUser sysUser = sysUserService.FindDistinctByUsername(username);
        if(sysUser!=null) {
            return sysUserService.ToAuthUser(sysUser);
        }
        else
        {
            return null;
            /*UserAuthService one = new UserAuthService();
            one.setDisplayname("已注销账户");
            return one;*/
        }
    }

    @Override
    public List<AuthRole> GetGrantedRoles(String systemname, String userkey) {
        return null;
    }

    @Override
    public String GetSaltValueByUserName(String username) {
        return sysUserService.FindDistinctByUsername(username).getSalt();
    }

    @Override
    public AuthUser GetByUserKey(String userkey) {
        SysUser sysUser = sysUserService.FindDistinctById(userkey);
        if(sysUser!=null)
        {
            return sysUserService.ToAuthUser(sysUser);
        }
        else
        {
            return null;
        }
    }

    @Override
    public Page<AuthUser> FindByFilters(String keywords,Pageable pageable) {
        return sysUserService.FindPageByKeyWords('%'+ keywords+'%',pageable).map(e-> sysUserService.ToAuthUser(e));
    }

    private String getMd5(String mm){
        String test = null;
        try {
            test = DigestUtils.md5DigestAsHex(mm.getBytes("UTF-16le"));
        } catch (UnsupportedEncodingException e) {
            test = "";
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder(test);
        int l = sb.length()%2 ==0? (sb.length()/2-1):(sb.length()/2);
        for(int i =l; i>0 ;i--)
        {
            sb.insert(i*2,"-");
        }
        //System.out.println(sb.toString().toUpperCase());
        return sb.toString().toUpperCase();
    }
}
